package com.yxjx.duoxue.course;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.thirdparty.neutra.autoscrollloopviewpager.AutoScrollLoopViewPager;
import com.thirdparty.trinea.autoscrollviewpager.AutoScrollViewPager;
import com.umeng.update.UmengUpdateAgent;
import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.HomePageActivity;
import com.yxjx.duoxue.PageLoading;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.activities.ActivityIntroductionActivity;
import com.yxjx.duoxue.ad.AdPageActivity;
import com.yxjx.duoxue.course.datamodel.HomeAllAdData;
import com.yxjx.duoxue.customview.HomeAllAdsView;
import com.yxjx.duoxue.customview.YxGridView;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.datamodel.DMAdItem;
import com.yxjx.duoxue.datamodel.DMBaiduLocation;
import com.yxjx.duoxue.datamodel.DMCourseCategory;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.message.UnreadStatus;
import com.yxjx.duoxue.mta.stat.StatHelper;
import com.yxjx.duoxue.my.UserLoginActivity;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;
import com.yxjx.duoxue.versionupdate.ConfigUpdateVersionDTS;
import com.yxjx.duoxue.versionupdate.UpdateManager;


public class HomePageFragment extends Fragment {
	
	private static final int TYPE_GENERAL_ITEM = 0;
	private static final int TYPE_MORE_COURSE = 1;
	private static final int TYPE_ITEM_COUNT = 2;
	
	public static final int COUNT_GENERAL_ITEM = 20;
	public static final int COUNT_MORE_COURSE = 1;
	public static final int COUNT_TOTAL = COUNT_GENERAL_ITEM + COUNT_MORE_COURSE;
	public static final int COURSE_START_INDEX = 0;
	
	// 列表页面传入的课程类别名称
	public static final String EXTRA_SEARCH_CATEGORY = "EXTRA_SEARCH_CATEGORY";
	public static final String EXTRA_SEARCH_CATEGORY_ID = "EXTRA_SEARCH_CATEGORY_ID";
	
	public static final int MSG_AD_NEXT = 1;
	public static final int MSG_LOAD_AD_TYPES_COURSES_DONE = 2;	// 加载广告，课程类别，推荐课程完毕
	public static final int MSG_CODE_PULL_REFRESH_DONE = 3; // 下拉刷新首页结束
	// 从多学服务器上检查是否要强制更新结束， 如果时强制更新，弹出去更新的dialog，永不消失。
	// 非强制更新，调用requestLoadInfo.
	public static final int MSG_CHECK_FORCE_UPDATE_DONE = 4; 

	private MyAdapter courseListAdapter;
	
	// cache current fragment.
	private View rootView;
	
	// AD view pager
	private AutoScrollLoopViewPager adViewPager;
	public AdViewPagerAdapter adViewPagerAdapter;
	
	//推荐课程
	private ArrayList<DMCourse> mCourseArray;
	public View adView;
	private GridAdapter courseCategoriesAdapter;
	private Context mContext;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = inflater.inflate(R.layout.fragment_home_page, null);
			ptrScrollView = (PullToRefreshScrollView) rootView.findViewById(R.id.scroll);
			ptrScrollView.setMode(Mode.PULL_FROM_START);
			ptrScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
				@Override
				public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
					pullToRefersh();
				}
			});
			
			courseCategoriesGridView = (YxGridView) rootView
					.findViewById(R.id.myGrdiView);
			courseCategoriesAdapter = new GridAdapter();
			courseCategoriesGridView.setAdapter(courseCategoriesAdapter);
			courseCategoriesGridView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					DMCourseCategory item = courseCategoriesAdapter.getData().get(arg2);
					String text = UIHelper.getText(arg1, R.id.category_title);
					if (text.startsWith("全部")) {
						StatHelper.addKVEvent(mContext, "event_home_all_categories_click");
						startActivity(new Intent(mContext, DetailedCourseTypesActivity.class));
					} else {
						Intent intent = new Intent(mContext, CourseListActivity.class);
						intent.putExtra(EXTRA_SEARCH_CATEGORY_ID, item.getType());
						intent.putExtra(EXTRA_SEARCH_CATEGORY, item.getStr());
						StatHelper.addKVEvent(mContext, "event_home_category_click",
								"class", "" + item.getType());
						startActivity(intent);
					}

				}
			});
			
			// Ad View pager segment_home_page_ad 
			adViewPager = (AutoScrollLoopViewPager) rootView.findViewById(R.id.ad_pager);
			adView = rootView.findViewById(R.id.ad_indicator);
			Utils.logd("HomePageFragment loading ADs");
			
			courseList = (YxListView) rootView.findViewById(R.id.course_list);
			mContext = rootView.getContext();
			
			// 先检查是否需要强制更新再去尝试加载信息。
			checkForceUpdate();
			return rootView;
		} else {
			// used the cached one.
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
			return rootView;
		}
	}
	
	private ConfigUpdateVersionDTS queryForceUpdateFromWeb() {
		try {
			ConfigUpdateVersionDTS item = NwUtils.queryForceUpdateFromWeb(mContext);
			if (item == null) {
				return null;
			} else {
				return item;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void checkForceUpdate() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				showLoading();
				// 
				ConfigUpdateVersionDTS forceUpdate = queryForceUpdateFromWeb();
				mHandler.sendMessage(mHandler.obtainMessage(MSG_CHECK_FORCE_UPDATE_DONE, forceUpdate));
			}
		}).start();
	}
	
	// 版本更新
	private void handleForceUpdate(final ConfigUpdateVersionDTS forceUpdate) {
		if (forceUpdate != null && forceUpdate.getNeedUpdate() != 0) {
			Dialog dialog = new Dialog(mContext, R.style.MyDialog);
			View dlgView = LayoutInflater.from(mContext).inflate(R.layout.segment_force_update, null);
			UIHelper.setText(dlgView, R.id.text, forceUpdate.getDes());
			UIHelper.setClickListener(dlgView, R.id.update_now, new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					new UpdateManager(mContext, forceUpdate.getUpdateUrl()).showDownloadDialog();
				}
			});
			
			dialog.setContentView(dlgView);
			dialog.setCancelable(false);
			dialog.show();
			// 强制更新
			
		} else {
			UmengUpdateAgent.update(mContext);
			requestLoadInfo();
		}
	}

	// loading当前页面必须的信息。
	private void requestLoadInfo() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				showLoading();
				PageLoading.getAllAdsForce(mContext, true);
				PageLoading.getHomeCourseCategory(mContext);
				PageLoading.getRecommendCourses(mContext);
				PageLoading.queryNewMessages(mContext);
				mHandler.sendMessage(mHandler.obtainMessage(MSG_LOAD_AD_TYPES_COURSES_DONE));
			}
		}).start();
	}
	
	// 下拉刷新
	private void pullToRefersh() {
		StatHelper.addKVEvent(mContext, "event_home_refresh");
		new Thread(new Runnable() {
			@Override
			public void run() {
				PageLoading.getAllAdsForce(mContext, true);
				PageLoading.getRecommendCoursesForce(mContext, true);
				mHandler.sendMessage(mHandler.obtainMessage(MSG_CODE_PULL_REFRESH_DONE));
			}
		}).start();
	}
	
	class MyAdapter extends BaseAdapter {
		
		@Override
		public int getCount() {
			if (mCourseArray == null) {
				return 1;
			}
			return COUNT_TOTAL;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public int getItemViewType(int position) {
			if (position == getCount() - 1) {
				return TYPE_MORE_COURSE;
			} else {
				return TYPE_GENERAL_ITEM;
			} 
		}

		@Override
		public int getViewTypeCount() {
			return TYPE_ITEM_COUNT;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View item = null;
			if (getItemViewType(arg0) == TYPE_GENERAL_ITEM) {
				if (arg1 == null) {
					item = LayoutInflater.from(mContext).inflate(
							R.layout.item_course_list_home, null);
				} else {
					item = arg1;
				}
				// customized data
				int index = arg0 - COURSE_START_INDEX;
				final DMCourse course = mCourseArray.get(index);
				CourseListActivity.customizeCourseItem(item, course);
			} else if (getItemViewType(arg0) == TYPE_MORE_COURSE) {
				// more course
				if (arg1 == null) {
					item = LayoutInflater.from(mContext).inflate(R.layout.item_more_courses, null);
				} else {
					item = arg1;
				}
				UIHelper.setText(item, R.id.text, "查看全部课程");
			}
			return item;
		}


	}
	
	
	private SharedPreferences.OnSharedPreferenceChangeListener mLocationChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {

		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreference, String key) {
			DMBaiduLocation location = DMBaiduLocation.forceGetLocationFromSharedPreference(mContext);
			onRelocationDone(location);
		}
	};
	
	private void onRelocationDone(DMBaiduLocation location) {
		courseListAdapter.notifyDataSetInvalidated();
	}
	
	private void initAdIndicator(LinearLayout indicator, int count) {
		indicator.removeAllViews();
		for (int i = 0; i < count; i++) {
			ImageView child = new ImageView(rootView.getContext());
			child.setBackgroundResource(R.drawable.home_ad_indicator);
			indicator.addView(child);
		}
	}
	
	private void setAdIndicator(LinearLayout indicator, int index) {
		for (int i = 0; i < indicator.getChildCount(); i++) {
			if (i == index) {
				indicator.getChildAt(i).setSelected(true);
			} else {
				indicator.getChildAt(i).setSelected(false);
			}
		}
	}
	
	private void initCoursesDone() {
		courseList.setVisibility(View.VISIBLE);
		mCourseArray = Globals.getInstance(mContext).getRecommendCourses();
		courseListAdapter = new MyAdapter();
		courseList.setAdapter(courseListAdapter);
		courseList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (courseListAdapter.getItemViewType(arg2) == TYPE_GENERAL_ITEM) {
					// 推荐的课程
					int index = arg2 - COURSE_START_INDEX;
					
					Intent intent = new Intent();
					DMCourse course = mCourseArray.get(index);
					intent.putExtra(NwUtils.KEY_ID, "" + course.getCourseId());
					intent.setClass(mContext, CourseIntroductionActivity.class);
					
					intent.putExtra(Yx.KEY_OPEN_BY, Yx.VALUE_OPEN_BY_REF_COURSE);
					StatHelper.addKVEvent(mContext, "event_home_ref_course_click",
							StatHelper.KEY_COURSE, course.getCourseId());
					
					UIHelper.LoadingActivity(mContext, intent);
				} else if (courseListAdapter.getItemViewType(arg2) == TYPE_MORE_COURSE) {
					StatHelper.addKVEvent(mContext, "event_home_click_all_course");
					Intent intent = new Intent(mContext, CourseListActivity.class);
					startActivity(intent);

				}
			}
		});
		Utils.registerPreferenceChangeListener(mContext, mLocationChangeListener);

	}
	
	private void initHomeCourseCategories() {
		final ArrayList<DMCourseCategory> categories = Globals.getInstance(mContext).getHomeCourseCategories();
		courseCategoriesAdapter.setData(categories);
	}
	
	private void initAllAds() {
		HomeAllAdData allAdData = Globals.getInstance(mContext).getAllAdsData();
		if (allAdData != null) {
			initAdDone();
			initAllAdsData();
		}
	}
	
	// 更新扩展的广告区域。
	private void initAllAdsData() {
		HomeAllAdsView allAdsView = (HomeAllAdsView) rootView.findViewById(R.id.all_ads_view);
		allAdsView.updateUI();
	}

	private void initAdDone() {
		Utils.logd("HomePageFragment int AD Done before");
		HomeAllAdData allAdData = Globals.getInstance(mContext).getAllAdsData();
		final ArrayList<DMAdItem> datas = allAdData.getBannerAds();
		// set initialization in middle.
		if (datas == null || datas.size() == 0) {
			return;
		}
		final int stepLen = datas.size();

		Utils.logd("InfiniteLoopPagerAdapterWrapper new viewPager");
		adViewPagerAdapter = new AdViewPagerAdapter(datas);
		Utils.logd("InfiniteLoopPagerAdapterWrapper add viewPager");
		adViewPager.setAdapter(adViewPagerAdapter);
		
		adViewPager.setCurrentItem(0);
		final View finalItem = adView;
		adViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (arg1 == 0.0) {
					int indicatorIndex = arg0 % stepLen;
					setAdIndicator((LinearLayout) finalItem.findViewById(R.id.ad_indicator), indicatorIndex);
				}
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		// indicator
		if (stepLen > 0) {
			LinearLayout indicator = (LinearLayout) adView.findViewById(R.id.ad_indicator);
			initAdIndicator(indicator, stepLen);
		}
		adViewPager.stopAutoScroll();
		adViewPager.startAutoScroll(5000);
		adViewPager.setInterval(5000);
		adViewPager.setStopScrollWhenTouch(true);
		Utils.logd("HomePageFragment int AD Done end");
	}

	class AdViewPagerAdapter extends PagerAdapter {
		private ArrayList<DMAdItem> mDataList = new ArrayList<DMAdItem>();
		
		public AdViewPagerAdapter(ArrayList<DMAdItem> datas) {
			mDataList = datas;
		}
		
		@Override
		public int getCount() {
			return mDataList == null ? 0 : mDataList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			DMAdItem dataItem = mDataList.get(position);
			View item = LayoutInflater.from(mContext).inflate(R.layout.segment_home_page_ad, null);
			UIHelper.asyncLoadImage((ImageView) item.findViewById(R.id.ad_image), dataItem.getImageUrl(), R.drawable.default_banner);
	
			final DMAdItem adItem = dataItem;
			item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if (adItem.getAdType() == DMAdItem.AD_TYPE_COURSE) {
						Intent intent = new Intent(mContext,
								CourseIntroductionActivity.class);
						intent.putExtra(NwUtils.KEY_ID, adItem.getItemId());
						addMtaEvent(intent, StatHelper.KEY_COURSE + adItem.getItemId());
						UIHelper.LoadingActivity(mContext, intent);

					} else if (adItem.getAdType() == DMAdItem.AD_TYPE_ACTIVITY) {
						Intent intent = new Intent(mContext,
								ActivityIntroductionActivity.class); 
						intent.putExtra(Yx.EXTRA_DM_ACTIVITY_ID, "" + adItem.getItemId());
						addMtaEvent(intent, StatHelper.KEY_ACTIVITY + adItem.getItemId());
						UIHelper.LoadingActivity(mContext, intent);

					} else if (adItem.getAdType() == DMAdItem.AD_TYPE_H5) {
						if (adItem.getNeedLogin() == 1 && !Utils.isLogin(mContext)) {
							Intent intent = new Intent(mContext, UserLoginActivity.class);
							intent.putExtra(Yx.KEY_URL, adItem.getPageUrl());
							intent.putExtra(Yx.KEY_TITLE, adItem.getTitle());
							intent.putExtra(Yx.EXTRA_AD_OBJECT, adItem);
							addMtaEvent(intent, adItem.getPageUrl());
							getActivity().startActivityForResult(intent, HomePageActivity.REQUEST_CODE_LOGIN_FOR_H5_PAGE);
						} else {
							Intent intent = new Intent(mContext, AdPageActivity.class);
							intent.putExtra(Yx.KEY_TITLE, adItem.getTitle());
							intent.putExtra(Yx.KEY_URL, adItem.getPageUrl() 
									+ UserLoginResponse.getPhoneNum(mContext) + NwUtils.webTail());
							intent.putExtra(Yx.EXTRA_AD_OBJECT, adItem);
							addMtaEvent(intent, adItem.getPageUrl());
							startActivity(intent);
						}
					}
				}

				private void addMtaEvent(Intent intent, String params) {
					intent.putExtra(Yx.KEY_OPEN_BY, Yx.VALUE_OPEN_BY_AD_CLICK);
					StatHelper.addKVEvent(mContext, "event_home_ad_click",
							StatHelper.KEY_AD, params);
				}
			});
			
			container.addView(item);
			return item;
		}
		
	}

	class GridAdapter extends BaseAdapter {
		
		private ArrayList<DMCourseCategory> courseCategories;
		public void setData(ArrayList<DMCourseCategory> data) {
			courseCategories = data;
			notifyDataSetChanged();
		}
		
		public ArrayList<DMCourseCategory> getData() {
			return courseCategories;
		}
		
		@Override
		public int getCount() {
			return courseCategories == null ? 0 : courseCategories.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View item = arg1;
			if (item == null) {
				item = LayoutInflater.from(mContext).inflate(
						R.layout.item_home_grid_item, null);
			}
			DMCourseCategory category = courseCategories.get(arg0);
			String title = category.getStr();
			UIHelper.setText(item, R.id.category_title, category.getStr());
			if (title != null && title.startsWith("全部")) {
				UIHelper.setText(item, R.id.category_title, "全部分类");
			}
			UIHelper.asyncLoadImage((ImageView) item.findViewById(R.id.category_icon), category.getIconUrl(), R.drawable.btn_home_default);
			return item;
		}
	}
	
	private Handler mHandler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_CODE_PULL_REFRESH_DONE:
			case MSG_LOAD_AD_TYPES_COURSES_DONE:
				ArrayList<DMCourseCategory> categoriesArray = Globals.getInstance(mContext).getHomeCourseCategories();
				ArrayList<DMCourse> courseArray = Globals.getInstance(mContext).getRecommendCourses();
				
				if (categoriesArray == null || categoriesArray.size() == 0
						|| courseArray == null || courseArray.size() == 0) {
					// 提示重新加载
					showLoadingFail();
				} else {
					rootView.post(new Runnable() {
						
						@Override
						public void run() {
							initAllAds();
							initHomeCourseCategories();
							initCoursesDone();
							UIHelper.resetScroll(getActivity(), R.id.scroll);
							showPageInfo();
						}
					});
				}
				if (msg.what == MSG_CODE_PULL_REFRESH_DONE) {
					ptrScrollView.onRefreshComplete();
				}
				tryUpdateActionBar();
				break;
			case MSG_CHECK_FORCE_UPDATE_DONE:
				ConfigUpdateVersionDTS forceUpdate = (ConfigUpdateVersionDTS) msg.obj;
				handleForceUpdate(forceUpdate);
				break;
			default:
				super.handleMessage(msg);
				break;
			}
			
		};
		
	};
	private YxListView courseList;
	private YxGridView courseCategoriesGridView;
	private PullToRefreshScrollView ptrScrollView;
	
	private void showPageInfo() {
		if (rootView == null) return;
		rootView.post(new Runnable() {
			@Override
			public void run() {
				rootView.findViewById(R.id.root).setVisibility(View.VISIBLE);
				rootView.findViewById(R.id.loading).setVisibility(View.INVISIBLE);
				rootView.findViewById(R.id.loading_fail).setVisibility(
						View.INVISIBLE);				
			}
		});
		
	}

	private void showLoadingFail() {
		if (rootView == null) return;
		rootView.post(new Runnable() {
			@Override
			public void run() {
				rootView.findViewById(R.id.root).setVisibility(View.INVISIBLE);
				rootView.findViewById(R.id.loading).setVisibility(View.INVISIBLE);
				rootView.findViewById(R.id.loading_fail).setVisibility(
						View.VISIBLE);
			}
		});
	}

	private void showLoading() {
		if (rootView == null) return;
		rootView.post(new Runnable() {
			@Override
			public void run() {
				rootView.findViewById(R.id.root).setVisibility(View.INVISIBLE);
				rootView.findViewById(R.id.loading).setVisibility(View.VISIBLE);
				rootView.findViewById(R.id.loading_fail).setVisibility(
						View.INVISIBLE);
			}
		});
	}
	
	/**
	 * 网络加载失败重新刷新页面
	 * @param view
	 */
	public void toRefresh(View view) {
		requestLoadInfo();
	}
	
	private void tryUpdateActionBar() {
		tryUpdateActionBarInternal(getActivity(), R.id.settings);
	}
	
	public static void tryUpdateActionBarInternal(FragmentActivity activity, int resId) {
		if (activity == null || !(activity instanceof ActionBarActivity)) {
			return;
		}
		UnreadStatus unread = Globals.getInstance(activity).getUnread();
		boolean getUnread = unread == null ? false : unread.isHasNewMessage();
		((ActionBarActivity)activity).getSupportActionBar().getCustomView().findViewById(resId).setSelected(getUnread);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		tryUpdateActionBar();
		// 尝试开始轮播图
		adViewPager.stopAutoScroll();
		adViewPager.startAutoScroll(5000);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		// 尝试停止轮播图
		adViewPager.stopAutoScroll();
	}
	
}
